package com.liyuan.yiqixing.controller;

import com.liyuan.yiqixing.model.Discussion;
import com.liyuan.yiqixing.model.ResultJson;
import com.liyuan.yiqixing.model.User;
import com.liyuan.yiqixing.service.DiscussionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class PublishController {
    @Autowired
    DiscussionService discussionService;


    @PutMapping("/publish")
    public ResultJson<Object> publish(@RequestBody Discussion discussion){
        User user = (User) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        discussion.setUser(user.getId());
        discussionService.saveDiscussion(discussion);
        int discussionId = discussionService.selectLastInsertId();
        return new ResultJson<>(200,"publish "+ discussion.getTitle()+" succcess!!",discussionId);
    }
}
